//
//  SQFunctionViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/6.
//

#import "SQFunctionViewController.h"

/**作业申请*/
#import "SQApplyForViewController.h"
/**作业检查*/
#import "SQHomeWorkViewController.h"
/**专项检查*/
#import "SQSpecialViewController.h"
/**检查记录*/
#import "SQCheckRecordViewController.h"

/**隐患列表*/
#import "SQListViewController.h"
/**隐患登记*/
#import "SQRegistrationViewController.h"
/**隐患确认*/
#import "SQConfirmViewController.h"
/**隐患整改*/
#import "SQRectificationViewController.h"
/**隐患验收*/
#import "SQAcceptanceViewController.h"
/**b报警记录*/
#import "SQAlarmRecordViewController.h"
/**应急响应*/
#import "SQReleaseViewController.h"
/**应急救援*/
#import "SQEmergencyRescueViewController.h"
/**应急预案*/
#import "SQPlanViewController.h"

//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།
#import "SQKeyAlarmModel.h"
#import "SQaddPoliceRecord.h"
#import "AppDelegate+Tool.h"
#import "SQFunctionModel.h"
#import "SQTwoOuterModel.h"
#import "SQUpdateTwoOuterModel.h"

@interface SQFunctionViewController ()<BaseTableVCInterface>
@property ( strong ,nonatomic) SQKeyAlarmModel *keyAlarmModel;
/**两外人员弹窗*/
@property (strong, nonatomic) IBOutlet UIView *view_personnel;
@property (weak, nonatomic) IBOutlet UILabel *lab_num;

@property (weak, nonatomic) IBOutlet UITextField *tf_num;
@property (weak, nonatomic) IBOutlet UILabel *tf_name;
@property ( strong ,nonatomic) SQTwoOuterModel *twoOuterModel;
@property ( copy ,nonatomic) NSString *state;
@end

@implementation SQFunctionViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    [self getAlarmTel];
   
    LC_WEAK_SELF
    self.cellTitles = ^(NSString * data) {
        
         if ([data isEqualToString:@"作业申请"]){
            SQApplyForViewController * applyForVC = SQApplyForViewController.new;
            [weakSelf pushVC:applyForVC];
        }else if ([data isEqualToString:@"作业检查"]) {
            SQHomeWorkViewController * homeWorkVC = SQHomeWorkViewController.new;
            [weakSelf pushVC:homeWorkVC];
        }else if ([data isEqualToString:@"专项检查"]) {
            SQSpecialViewController * specialVC = SQSpecialViewController.new;
            [weakSelf pushVC:specialVC];
        }else if ([data isEqualToString:@"检查记录"]) {
            SQCheckRecordViewController * checkRecordVC = SQCheckRecordViewController.new;
            [weakSelf pushVC:checkRecordVC];
        }else if ([data isEqualToString:@"隐患列表"]) {
            SQListViewController * listVC = SQListViewController.new;
            [weakSelf pushVC:listVC];
        }else if ([data isEqualToString:@"独立隐患"]){
            [weakSelf.tabBarController setSelectedIndex:1];
        }else if([data isEqualToString:@"隐患登记"]){
            SQRegistrationViewController * registrationVC = SQRegistrationViewController.new;
            [weakSelf pushVC:registrationVC];
        }else if ([data isEqualToString:@"隐患确认"]) {
            SQConfirmViewController * confirmVC = SQConfirmViewController.new;
            [weakSelf pushVC:confirmVC];
        }else if ([data isEqualToString:@"隐患整改"]){
            SQRectificationViewController * rectificationVC = SQRectificationViewController.new;
            [weakSelf pushVC:rectificationVC];
        }else if ([data isEqualToString:@"隐患验收"]){
            SQAcceptanceViewController * acceptanceVC = SQAcceptanceViewController.new;
            [weakSelf pushVC:acceptanceVC];
        }else if ([data isEqualToString:@"一键报警"]){
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"报警电话"
                                                                               message:nil
                                                                        preferredStyle:UIAlertControllerStyleActionSheet];
            for (int i = 0; i < weakSelf.keyAlarmModel.data.count; i ++) {
                SQKeyAlarmModelData * dataModel = weakSelf.keyAlarmModel.data[i];
                UIAlertAction* telAction = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@-%@-%@",dataModel.at_organization,dataModel.at_name,dataModel.at_phone] style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                    
                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",dataModel.at_phone]];

                    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {

                    }];
                    [weakSelf addPoliceRecord:dataModel];
                    
                }];
                [alert addAction:telAction];
            }
            
            
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      NSLog(@"action = %@", action);
                                                                  }];
            [alert addAction:cancelAction];
//
            [weakSelf presentViewController:alert animated:YES completion:nil];
        }else if ([data isEqualToString:@"报警记录"]){
            SQAlarmRecordViewController * alarmRecordVC = SQAlarmRecordViewController.new;
            [weakSelf pushVC:alarmRecordVC];
        }else if ([data isEqualToString:@"应急响应"]){
            SQReleaseViewController * releaseVC = SQReleaseViewController.new;
            [weakSelf pushVC:releaseVC];
        }else if ([data isEqualToString:@"应急救援"]){
            SQEmergencyRescueViewController * emergencyRescueVC = SQEmergencyRescueViewController.new;
            [weakSelf pushVC:emergencyRescueVC];
        }else if ([data isEqualToString:@"应急预案"]){
            SQPlanViewController * planVC = SQPlanViewController.new;
            [weakSelf pushVC:planVC];
        }else if ([data isEqualToString:@"外企人员"]){
            weakSelf.state = @"1";
            [weakSelf getFunctionModel:@"1"];
           
        }else if ([data isEqualToString:@"外包人员"]){
            weakSelf.state = @"2";
            [weakSelf getFunctionModel:@"2"];
          
        }
    };
    
    
}

-(void)getFunctionModel:(NSString *)Type{
    [SQFunctionModel getDataWithParams:@{@"fp_enterprise":MEnterpriseId,@"fp_person":UID} HUD:NO  WithCallBack:^(SQFunctionModel * data) {
        if ([data.isdouble isEqualToString:@"False"]) {
            [CCToast showMessage:@"当前账号没有两外人员权限"];
        }else{
            [SQTwoOuterModel getDataWithParams:@{@"em_id":MEnterpriseId} WithCallBack:^(SQTwoOuterModel *  data) {
                self.twoOuterModel = data;
                if ([Type isEqualToString:@"1"]) {
                    self.lab_num.text = [NSString stringWithFormat:@"当前外企人员数: %@",data.foreign_enterprise];
                    self.tf_name.text = @"填报外企人数";
                    self.tf_num.placeholder = @"请输入外企人数";
                
                }else if ([Type isEqualToString:@"2"]){
                    self.lab_num.text = [NSString stringWithFormat:@"当前外包人员数: %@",data.outsource];
                    self.tf_name.text = @"填报外包人数";
                    self.tf_num.placeholder = @"请输入外包人数";
                };
                [AppDelegate addSubview:self.view_personnel];
            }];
            
            
           
            
        }
    }];
}


-(void)updatetwoOuter:(NSString *)outsource foreign_enterprise:(NSString *)foreign_enterprise {
    [SQUpdateTwoOuterModel getDataWithParams:@{@"em_id":MEnterpriseId,@"outsource":outsource,@"foreign_enterprise":foreign_enterprise} WithCallBack:^(SQUpdateTwoOuterModel * data) {
        [CCToast showMessage:@"更新成功"];
        [self.view_personnel removeFromSuperview];
    }];
}


-(void)getAlarmTel{
    [SQKeyAlarmModel getDataWithParams:@{@"at_enterprise":MEnterpriseId} WithCallBack:^(SQKeyAlarmModel * data) {
        self.keyAlarmModel = data;
        
    }];
}

-(void)addPoliceRecord:(SQKeyAlarmModelData *)dataModel{
    [SQaddPoliceRecord getDataWithParams:@{@"apr_alarm_person":MName,@"apr_alarm_organization":[self join:@[[self isEmptyNull:MOneText],[self isEmptyNull:MTwoText],[self isEmptyNull:MThreeText],[self isEmptyNull:MFourText]]],@"apr_alarm_phone":MPhone,@"apr_receive_person":dataModel.at_name,@"apr_receive_organization":dataModel.at_organization,@"apr_receive_phone":
                                               dataModel.at_phone,@"apr_state":@"0",@"apr_enterprise":MEnterpriseId} WithCallBack:^(SQaddPoliceRecord * data) {
        
        }];
}

- (NSString *)join:(NSArray<NSString *> *)strs{
    NSString *str = @"";
    for (NSString *key in strs) {
        if (key.length > 0) {
            str = [str stringByAppendingFormat:@"%@-",key];
        }
    }
    if (!str.length)return @"";
    return [str substringToIndex:str.length - 1];
}

- (NSString *)isEmptyNull:(NSString *)string{
    if (string == nil || string == NULL) {
        string = @"";
    }
    return string;
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return @[@""];
}



-(NSInteger)rowsCount:(NSInteger)section{
    return 4;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQFunctionTableViewCell"]];
}

- (IBAction)clickSubmit:(UIButton *)sender {
   
    if ([self.state isEqualToString:@"1"]) {
        
        if ([self.tf_num.text isEqualToString:@""]) {
            [CCToast showMessage:@"请输入外企人员数量"];
            return;
        }else{
            [self updatetwoOuter:self.tf_num.text foreign_enterprise:self.twoOuterModel.foreign_enterprise];
        }
      
    
    }else if ([self.state isEqualToString:@"2"]){
        if ([self.tf_num.text isEqualToString:@""]) {
            [CCToast showMessage:@"请输入外包人员数量"];
            return;
        }else{
            [self updatetwoOuter:self.twoOuterModel.outsource foreign_enterprise:self.tf_num.text];
        }
        
    
    };
}

- (IBAction)clickCancel:(UIButton *)sender {
    [self.view_personnel removeFromSuperview];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
